#include "loginwgt.h"
#include<QPushButton>
#include<QMouseEvent>
#include<QVBoxLayout>
#include<QLabel>
#include<QHBoxLayout>
#include<QLineEdit>
#include"UI/tool/styleloader.h"
#include"ICMS.h"
#include"appconfig.h"

LoginWgt::LoginWgt(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(300,400);
    initLayout();
}

void LoginWgt::slot_Operator(const REQUESTTYPE type, bool result)
{
    switch (type) {
    case REGISTER:
        handleRegister(result);
    case LOGIN:
        handleLogin(result);
        break;
    default:
        break;

    }

}
void LoginWgt::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton&&isPressed)
    {
        if(!qobject_cast<QPushButton*>(childAt(event->pos())))//判读当前鼠标位置不是按钮
        {
            move(event->globalPos()-point);
        }
    }
    QWidget::mouseMoveEvent(event);
    qDebug()<<"鼠标移动";
}
void LoginWgt:: mousePressEvent(QMouseEvent *event)
{
    if(!qobject_cast<QPushButton*>(childAt(event->pos())))
    {
        isPressed=true;
        point=event->globalPos()-this->frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
    qDebug()<<"鼠标按下";
}
void LoginWgt:: mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        isPressed=false;
    }
    qDebug()<<"鼠标释放";
}

void LoginWgt::slot_MediaInfo(const QVector<QVariantMap> mediainfo)
{
    //
    if(!mediainfo.isEmpty())
    {
        ICMS*icms=new ICMS();
        icms->show();
        hide();
    }

}

void LoginWgt::handleLogin(bool result)
{
    if(result)
    {
        AppConfig*app=AppConfig::instance();
        HttpHelper * httpHelper=HttpHelper::instance();
        QVector<QVariantMap>infos=app->getVideoFiles();
        for(const QVariantMap&video:infos)
        {
            if(video.contains("mediaUrl")&&video.contains("mediaTitle"))
            {
                QString mediaUrl=video["mediaUrl"].toString();
                QString mediaTitle=video["mediaTitle"].toString();
                httpHelper->slot_AddmediaInfo(video);
                qDebug()<<"媒体文件："<<mediaTitle<<"路径:"<<mediaUrl;
            }
            else {
                qDebug()<<"缺少必要字段";
            }

        }
        httpHelper->slot_GetmediaInfo();
    }
    else {
        qDebug()<<"登录失败";
    }

}

void LoginWgt::handleRegister(bool result)
{
    if(result)
    {
        stackedWgt->setCurrentWidget(loginWgt);
    }
    else {
        qDebug()<<"注册失败";
    }

}

void LoginWgt::initLayout()
{
    //主布局
    QVBoxLayout*mainlayout=new QVBoxLayout(this);
    //创建公共窗口（标题窗口，由于关闭程序）
    QWidget*headerWgt=new QWidget(this);
    headerWgt->setFixedSize(300,150);
    headerWgt->setWindowFlag(Qt::FramelessWindowHint);
    headerWgt->setAttribute(Qt::WA_StyledBackground);

    //登录用户头像
    QLabel*iconLabel=new QLabel(headerWgt);
    iconLabel->setAlignment(Qt::AlignCenter);
    QPushButton*closeBtn=new QPushButton(headerWgt);
    iconLabel->setFixedSize(80,80);
    closeBtn->setFixedSize(30,30);
    //头像设置图片
    iconLabel->setPixmap(QPixmap("pash"));//把path改成自己的图片的路径
    //图片缩放
    iconLabel->setScaledContents(true);
    //关闭按钮绑定信息与槽
    connect(closeBtn,&QPushButton::clicked,this,[this]()
    {
        close();

    });
    //headerWgt布局
    QVBoxLayout*headerlayout=new QVBoxLayout(headerWgt);
    headerlayout->setContentsMargins(0,0,0,0);
    headerlayout->setSpacing(0);
    //顶部水平布局
    QHBoxLayout*toplayout=new QHBoxLayout;
    toplayout->setContentsMargins(0,0,0,0);
    toplayout->addStretch();
    toplayout->addWidget(closeBtn);
    headerlayout->addLayout(toplayout);

    //左键间隔
    headerlayout->addStretch();

    //图标布局
    QHBoxLayout*iconlayout=new QHBoxLayout();
    iconlayout->setContentsMargins(0,0,0,0);
    iconlayout->addStretch();
    iconlayout->addWidget(iconLabel);
    iconlayout->addLayout(iconlayout);

    headerWgt->setLayout(headerlayout);

    //stackedWidge
    stackedWgt=new QStackedWidget(this);
    stackedWgt->setFixedSize(300,250);
    stackedWgt->setWindowFlag(Qt::FramelessWindowHint);
    stackedWgt->setAttribute(Qt::WA_StyledBackground);

    mainlayout->setSpacing(0);
    mainlayout->addWidget(headerWgt);
    mainlayout->addWidget(stackedWgt);
    mainlayout->setContentsMargins(0,0,0,0);
    //登录窗口
    loginWgt=new QWidget(stackedWgt);
    registerWgt=new QWidget(stackedWgt);


    loginWgt->setFixedSize(300,250);
    loginWgt->setWindowFlag(Qt::FramelessWindowHint);
    loginWgt->setAttribute(Qt::WA_StyledBackground);

    registerWgt->setFixedSize(300,250);
    registerWgt->setWindowFlag(Qt::FramelessWindowHint);
    registerWgt->setAttribute(Qt::WA_StyledBackground);

    //添加登录窗口控件
    QLineEdit*accout=new QLineEdit(loginWgt);
    QLineEdit*passwd=new QLineEdit(loginWgt);
    QPushButton*loginBth=new QPushButton("登录",loginWgt);
    QPushButton*registerBtn=new QPushButton("没有账户？去注册",loginWgt);

    accout->setFixedSize(250,40);
    passwd->setFixedSize(250,40);
    loginBth->setFixedSize(250,40);
    registerBtn->setFixedSize(250,40);

    accout->setPlaceholderText("username");
    passwd->setPlaceholderText("passwd");
    passwd->setEchoMode(QLineEdit::Password);

    //登录窗口子控价布局
    QVBoxLayout*loginlayout=new QVBoxLayout(loginWgt);
    loginlayout->setAlignment(Qt::AlignCenter);
    loginlayout->setSpacing(10);
    loginlayout->addWidget(accout);
    loginlayout->addWidget(passwd);
    loginlayout->addStretch();
    loginlayout->addWidget(loginBth);
    loginlayout->addWidget(registerBtn);
    loginlayout->addStretch();
    loginlayout->setContentsMargins(0,0,0,0);
    loginWgt->setLayout(loginlayout);
    //注册子控件
    QLineEdit*reusername=new QLineEdit(registerWgt);
    QLineEdit*repasswd=new QLineEdit(registerWgt);
    QPushButton*startRegisterBtn=new QPushButton("开始注册",registerWgt);
    QPushButton*reloginBth=new QPushButton("返回登录",registerWgt);

    connect(loginBth,&QPushButton::clicked,this,[=]()
    {
        QVariantMap infoMap;
        QString username=reusername->text();
        QString userpasswd=repasswd->text();
        if(!username.isEmpty()&&!userpasswd.isEmpty())
        {
            infoMap["userName"]=username;
            infoMap["password"]=userpasswd;
            infoMap["nickName"]="test";
            HttpHelper::instance()->slot_Login(infoMap);
        }
        else
        {
            qDebug()<<"账户密码不能为空";
        }
    });
    connect(loginBth,&QPushButton::clicked,this,[=]()
    {
        QVariantMap infoMap;
        QString username=reusername->text();
        QString userpasswd=repasswd->text();
        if(!username.isEmpty()&&!userpasswd.isEmpty())
        {
            infoMap["userName"]=username;
            infoMap["password"]=userpasswd;
            infoMap["nickName"]="test";
            HttpHelper::instance()->slot_Register(infoMap);
        }
        else
        {
            qDebug()<<"账户密码不能为空";
        }
    });

    connect(reloginBth,&QPushButton::clicked,this,[this]()
    {
        stackedWgt->setCurrentWidget(loginWgt);
    });
    //设置注册窗口子控件大小
    reusername->setFixedSize(250,40);
    repasswd->setFixedSize(250,40);
    startRegisterBtn->setFixedSize(250,40);
    reloginBth->setFixedSize(250,40);

    //编辑框设置占位符
    reusername->setPlaceholderText("reusername");
    repasswd->setPlaceholderText("repawsswd");
    repasswd->setEchoMode(QLineEdit::Password);

    //注册窗口设置布局
    QVBoxLayout*registerlayout=new QVBoxLayout(registerWgt);
    registerlayout->setAlignment(Qt::AlignCenter);
    registerlayout->setSpacing(10);
    registerlayout->addWidget(reusername);
    registerlayout->addWidget(repasswd);
    registerlayout->addStretch();
    registerlayout->addWidget(startRegisterBtn);
    registerlayout->addWidget(reloginBth);
    registerlayout->addStretch();
    registerlayout->setContentsMargins(0,0,0,0);
    registerWgt->setLayout(registerlayout);

    //stackedwidget添加子窗口
    stackedWgt->addWidget(loginWgt);
     stackedWgt->addWidget(registerWgt);

     connect(registerBtn,&QPushButton::clicked,this,[this](){
         stackedWgt->setCurrentWidget(registerWgt);
          });
     stackedWgt->setCurrentWidget(loginWgt);

     this->setLayout(mainlayout);

}

